package cn.ycxy.lbl.posts.service.impl;


import cn.ycxy.lbl.pojo.order.domain.LblCsOrder;
import cn.ycxy.lbl.pojo.order.dto.OrderListDTO;
import cn.ycxy.lbl.pojo.order.vo.OrderListVO;
import cn.ycxy.lbl.pojo.posts.constant.DiscussPostsConstant;
import cn.ycxy.lbl.pojo.posts.domain.LblCsDiscussPost;
import cn.ycxy.lbl.posts.dao.DiscussPostsDao;
import cn.ycxy.lbl.posts.dao.OrderDao;
import cn.ycxy.lbl.posts.service.IDiscussPostsService;
import cn.ycxy.lbl.posts.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 订单数据表 服务实现类
 * </p>
 *
 * @author BaoL
 * @since 2023-04-12
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao, LblCsOrder> implements IOrderService, DiscussPostsConstant {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private DiscussPostsDao discussPostsDao;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public List<OrderListVO> selectOrderByBuy(OrderListDTO orderListDTO) {
        orderListDTO.calculateStartIndex();
        return orderDao.selectOrderByBuy(orderListDTO);
    }

    @Override
    public List<OrderListVO> selectOrderBySale(OrderListDTO orderListDTO) {
        orderListDTO.calculateStartIndex();
        return orderDao.selectOrderBySale(orderListDTO);
    }

    @Override
    public boolean modifyDealState(LblCsOrder lblCsOrder) {
        boolean b = updateById(lblCsOrder);
        boolean b2 = true;
        //完成交易, 移除搜索
        if (lblCsOrder.getState() == 2){
            LblCsDiscussPost post = new LblCsDiscussPost();
            post.setId(lblCsOrder.getPostId());
            post.setType(POSTS_DEAL_TYPE);
            post.setIsFinishDeal(1);
            b2 = discussPostsDao.updateById(post) > 0;
            //使用rabbitmq异步发送消息, 把帖子从es中删除
            rabbitTemplate.convertAndSend(CS_POSTS_EX, CS_POSTS_DEL_RK, post);
        }
        return b && b2;
    }
}
